<?php
/*
 *      statement.class.php
 *
 *      Copyright 2010 Patrice Ferlet <metal3d@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

// $Id: statement.class.php 19 2010-12-14 22:46:23Z Metal3d $
// $HeadURL: http://valaya.googlecode.com/svn/trunk/utils/statement.class.php $

/**
 *  Statement is use by Db. This is the object class returned by every
 *  queries calls. It implements some basic methods as insert, update...
 *  to ease the data work.
 * */
class Statement extends PDOStatement {

	private $dbh;
	private $attrs;
	private $newfields;

	/**
	 * Create Statement object
	 *
	 * @param mixed $dbh
	 * */
	protected function __construct($dbh=false) {
		if(!$dbh) $dbh = DB::getInstance();
		$this->dbh = $dbh;
		$this->setFetchMode(PDO::FETCH_CLASS,'Statement',array($this->dbh));
	}

	/**
	 * Create a new Statement
	 *
	 * @param Db::instance $db
	 * @return Statement $empty_db_statement
	 * */
	public function create($dbh=false){
		$r = new self($dbh);
		return $r;
	}

	/**
	 * Fetch next value from statement after query
	 *
	 * @return Statement $record
	 * */
	public function fetch(){
		$obj = parent::fetch();
		$this->attrs = $obj->attrs;
		$this->newfields = $this->attrs;
		return $obj;
	}

	/**
	 * Update a table with properties values set on this Statement
	 *
	 * @parame string $table
	 * @return Statement $statement
	 * */
	public function update($table){
		$props   = Db::$datadict[$table];
		var_dump($props);
		$set = $where = $fields = array();

		//set values
		var_dump($this->newfields);
		foreach($props['fields'] as $name){
			if(!isset($this->newfields->{$name})) continue;
			$set[] = ' '.$name.' = ?';
			$first = false;
			$fields[] = $this->newfields->{$name};
		}

		//set ids...
		foreach (Db::$datadict[$table]['id'] as $id) {
			$where[] = $id.'= ?';
			$fields[] = $this->attrs->{$id};
		}

		$query='update '.$table.' set '. implode(',', $set).' where '.implode(' and ',$where);
		return $this->dbh->query($query,$fields);
	}

	/**
	 * Insert new entry in table with values set in properties
	 *
	 * @param string $table
	 * @return Statement $statement
	 * */
	public function insert($table){
		$props   = Db::$datadict[$table];
		$set = '';
		$where="";
		$values = $fields = $q = array();

		foreach($props as $name){
			if(!isset($this->newfields->{$name})) continue;
			$fields[] = $name;
			$q[]='?';
			$values[] = $this->newfields->{$name};
		}

		$query='insert into '.$table.' ('.implode(',',$fields).') values ('.implode(',',$q).')';
		return $this->dbh->query($query, $values);

	}

	public function __set($attr,$value) {
		if(!$this->newfields) $this->newfields = new stdClass;
		$this->newfields->$attr = $value;
		if(!isset($this->attrs))  $this->attrs = new stdClass;
		if(!isset($this->attrs->$attr))  $this->attrs->$attr = $value;

	}

	public function __get($attr) {
		return $this->newfields->$attr;
	}
}
